<?php
namespace App\Repository;

use App\Model\Product;

class ProductRepository extends \HyperfStore\Repository
{
    // 放入模型
    public function build()
    {
        return new Product;
    }
    
    // 算出金额
    public function makeMoney($count)
    {
        return $this->model->money * $count;
    }
    
    // 验证库存
    public function hasCount($count)
    {
        if($count < 1)
        {
            return 0;
        }
        
        return $this->model->count >= $count ? $count : 0;
    }
    
    // 使用库存
    public function makeCount($count)
    {
        if($this->hasCount($count))
        {
            return $this->model->decrement('count', $count);
        }
    }
    
    // 限制时间
    public function isDay()
    {
        if($this->model->day)
        {
            return time() - ($this->model->day * 60 * 60 * 24);
        }
    }
    
    // 限制数量
    public function isDayCount($count)
    {
        return $this->model->day_count < $count;
    }
    
    // 删除商品
    public function delete()
    {
        return $this->model->delete();
    }
}